---
title: "Language Barriers (PB Replication)"
author: "Yamil R. Velez"
date: '2023-02-02'
output:
  html_document: default
  pdf_document: default
---

## Table 1

```{r, echo = FALSE, include=FALSE}
# Load packages
library(tidyverse)
library(TAM)
library(ggplot2)

# Load dataframes
load("hsi_data.RData")
load("lucid_data.RData")
load("cr1_data.RData")
load("cr2_data.RData")

# Estimate models
source("hsi_models.R")
source("lucid_models.R")
source("cr1_models.R") 
source("cr2_models.R") 
```

```{r}
# Create LR test table
source("lr_tests.R")
```

## Figure 1

```{r warning=FALSE, fig.width = 20, fig.height=20}
source("plot_dif.R")

dif_plot
```

## Table 2

```{r}
source("predict_dif.R")
```

## Figure 4

```{r warning = FALSE, fig.width = 10}
source("plot_dif.R")
source("addl_figures.R")
pk.plot
dif.plot2
```

## Appendix C

```{r, include=FALSE}
# Load data
load("cr1_data_ef.RData")
source("cr1_models_englishfirst.R")

load("cr1_data_sf.RData")
source("cr1_models_spanishfirst.R")

load("cr2_data_ef.RData")
source("cr2_models_englishfirst.R")

load("cr2_data_sf.RData")
source("cr2_models_spanishfirst.R")

```

```{r}
# Conduct LR tests
source("lr_test_first_language.R")
```

## Appendix D

```{r, echo = F, include=FALSE}
# Load dataframes
load("hsi_data.RData")
load("lucid_data.RData")
load("cr1_data.RData")
load("cr2_data.RData")

# Estimate models
source("hsi_models.R")
source("lucid_models_1pl.R")
source("cr1_models_1pl.R")
source("cr2_models_1pl.R")
```

```{r}
# Create LR test table
bind_cols(Sample = c(rep("HSI", 4), 
                     rep("Lucid", 3), 
                     rep("CloudResearch 1", 4),
                     rep("CloudResearch 2", 2))) %>%
 bind_cols(Outcome = c("Panethnic Identity",
                       "Americanism Beliefs",
                       "Partisan Identity",
                       "Immigration Opinion",
                       "Panethnic Identity",
                       "Trans Attitudes",
                       "Immigration Opinion",
                       "Panethnic Identity",
                       "Efficacy Beliefs",
                       "Minority Representation",
                       "Political Knowledge",
                       "Political Knowledge",
                       "Ideology"),
           Model = c("PCM", "PCM", "PCM", 
                     "NRM", "PCM", "PCM",
                     "PCM", "PCM","PCM", "PCM",
                     "1PL", "1PL", "1PL")) %>%
 bind_cols(lapply(list(hsi_panethnic_lr, 
                       hsi_american_lr, 
                       hsi_party_lr, 
                       hsi_immigration_lr, 
                       lucid_panethnic_lr, 
                       lucid_attp_lr, 
                       lucid_immigration_lr, 
                       cr1_panethnic_lr,
                       cr1_efficacy_lr, 
                       cr1_minrep_lr, 
                       cr1_pkscale_lr,
                       cr2_pkscale_lr,
                       cr2_ideology_lr), function(x) 
                        x$LRtest[3:5]) %>%
            bind_rows()) %>%
 mutate(p = round(p, 3), 
        df = paste(round(df, 1))) %>% 
 xtable() %>% print(., include.rownames = F)
```

## Appendix E

```{r, include=FALSE}
load("hsi_data.RData")
load("lucid_data.RData")
load("cr1_data.RData")
load("cr2_data.RData")

# Estimate models
source("hsi_models.R")
source("lucid_models.R")
source("cr1_models.R")
source("cr2_models.R")
```

```{r}
# Use plot code to extract estimates
source("plot_dif.R", verbose = F)
source("plot_disc.R")
disc.plot
```


## Appendix H

```{r}
library(rio)
library(xtable)

print(xtable(import("translation_assessment.csv") %>% 
 dplyr::group_by(model) %>%
 dplyr::summarize(`Expert Rating 1` = mean(overall_expert_1, na.rm = T),
                  `Expert Rating 2` = mean(overall_expert_2, na.rm = T),
           `Non-Expert Rating` = mean(overall)) %>%
  arrange(`Expert Rating 1`)), include.rownames = F)
```

## Appendix I

```{r}
source("predict_dif.R")

etable(feols(m_diff ~ 
              minmax(count_e-count_s) +
              minmax(flesch_e-huerta_s) | 
              model + study, translation_df),
       tex = T)
```
